package com.hainatong.shiphelper.module.warehouse.viewmodel;

import java.util.ArrayList;
import java.util.List;

import com.hainatong.shiphelper.api.ApiResult;
import com.hainatong.shiphelper.api.ApiService;
import com.hainatong.shiphelper.api.VMObserver;
import com.hainatong.shiphelper.module.warehouse.entity.*;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.LiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.disposables.Disposable;

public class LinYongViewModel extends BaseViewModel
{

    public LinYongViewModel(@NonNull Application application)
    {
        super(application);
    }

    private MutableLiveData<LiveResult<Object>> chukuResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<Object>> getChukuResult()
    {
        return chukuResult;
    }

    /**
     *  出库 任务
     * @param chukuGoods
     */
    public void chukuTask(ChuKuRaramBean[] chukuGoods )
    {
        ApiService.Builder.getChanBoServer()
                .chuKuTask( chukuGoods )
                .compose( RxSchedulersUtils.Obs_io_main() )
                .subscribe(new VMObserver<ApiResult<Object>>()
                {
                    @Override
                    public void onStart(Disposable d)
                    {
                        addDisposable( d );
                    }

                    @Override
                    public void onError(int code, String errorMsg)
                    {
                        getChukuResult().postValue( new LiveResult<>( false ,errorMsg ) );
                    }

                    @Override
                    public void onSuccess(ApiResult<Object> result)
                    {
                        //{"data":null,"status":true,"code":"200","message":null}
                        if( result.isOk() )
                        {
                            getChukuResult().postValue( new LiveResult<>( true , result.getData() ) );
                        }
                        else
                        {
                            getChukuResult().postValue( new LiveResult<>( false , result.getMessage() ) );
                        }
                    }
                });
    }


    private MutableLiveData<LiveResult<List<ShipOrgBean>>> orgUserResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<List<ShipOrgBean>>> getOrgUserResult()
    {
        return orgUserResult;
    }

    /**
     *  查询  人员 组织
     * @param lan
     */
    public void queryOrgUserListByLan(String lan)
    {
        ApiService.Builder.getChanBoServer()
                          .getOrgUsers(lan)
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<List<ShipOrgBean>>>() {
                              @Override
                              public void onStart(Disposable d)
                              {
                                 addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getOrgUserResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<List<ShipOrgBean>> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getOrgUserResult().postValue( new LiveResult<>( true ,result.getData() ) );
                                  }
                                  else
                                  {
                                      getOrgUserResult().postValue( new LiveResult<>( false ,result.getMessage() ) );
                                  }
                              }
                          });
    }






    /**
     *  获取 level
     * @param shipOrgBeans
     * @return
     */
    public static List<String> getDepartListInOrg(List<ShipOrgBean> shipOrgBeans)
    {
        List<String> levelList = new ArrayList<>();
        if( shipOrgBeans != null && !shipOrgBeans.isEmpty() )
        {
            for ( ShipOrgBean orgBean : shipOrgBeans )
            {
                levelList.add( orgBean.getLevel() );
            }
        }
        return levelList;
    }

    /**
     *  获取 job  职能 列表
     * @param shipOrgBeans
     * @param levelStr
     * @return
     */
    public static List<String> getJobListOrg(List<ShipOrgBean> shipOrgBeans ,String levelStr)
    {
        List<String> jobList = new ArrayList<>();

        if( shipOrgBeans != null && !shipOrgBeans.isEmpty() )
        {
            for( ShipOrgBean orgBean : shipOrgBeans  )
            {
                 if(  orgBean.getLevel().equals(levelStr)   )
                 {
                    if(  orgBean.getChildren() != null && !orgBean.getChildren().isEmpty()  )
                    {
                         List<OrgUser> orgUsers = orgBean.getChildren();
                         for(int p = 0 ; p < orgUsers.size() ; p++ )
                         {
                             if( jobList.isEmpty() )
                             {
                                  jobList.add(  orgUsers.get( p ).getRoleName()    );
                             }
                             else
                             {
                               String roleName = orgUsers.get( p ).getRoleName();
                               if( !jobList.contains( roleName ) )
                               {
                                   jobList.add( roleName );
                               }
                             }
                         }
                    }
                 }
            }
        }

        return jobList;
    }


    /**
     *  user
     * @param shipOrgBeans
     * @param levelStr
     * @return
     */
    public static List<OrgUser> getOrgUser(List<ShipOrgBean> shipOrgBeans ,String levelStr)
    {
        List<OrgUser> userList = new ArrayList<>();

        if( shipOrgBeans != null && !shipOrgBeans.isEmpty() )
        {
            for( ShipOrgBean orgBean : shipOrgBeans  )
            {
                if( orgBean.getLevel().equals(levelStr) )
                {
                    if(  orgBean.getChildren() != null && !orgBean.getChildren().isEmpty()  )
                    {
                        List<OrgUser> orgUsers = orgBean.getChildren();
                        userList.addAll( orgUsers );
                    }
                }
            }
        }
        return userList;
    }

    /**
     *
     * @param userList
     * @return
     */
    public static List<String> getOrgUserNameList(List<OrgUser>  userList )
    {
        List<String> workerList = new ArrayList<>();

        for(OrgUser user :  userList)
        {
            workerList.add( user.getPostion() );
        }
        return workerList;
    }

}
